package edu.mju.route.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.route.dto.SearchRouteDto;
import edu.mju.route.entity.Route;
import edu.mju.route.service.RouteService;
import edu.mju.serviceutils.ResponseCode;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBL
 * @since 2020-12-30
 */
@RestController
@RequestMapping("/route/route")
@CrossOrigin
public class RouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 根据传入线路对象 新增、修改线路
     * @param route 传入参数
     * @return 自定义返回
     */
    @PostMapping("/updateRoute")
    public ResponseCode updateRoute(@RequestBody Route route){
        boolean b = routeService.saveOrUpdate(route);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    /**
     * 根据id逻辑删除线路
     * @param id 线路id
     * @return 自定义返回
     */
    @DeleteMapping("/deleteRoute/{id}")
    public ResponseCode deleteRoute(@PathVariable Integer id){
        boolean b = routeService.deleteRoute(id);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    /**
     *  根据id查询线路信息
     * @param id 线路id
     * @return 自定义返回
     */
    @GetMapping("/getRoute/{id}")
    public ResponseCode getRoute(@PathVariable Integer id){
        Route byId = routeService.getById(id);
        return ResponseCode.success().data("route",byId);
    }

    /**
     *  条件带分页 查询线路
     * @param page 当前页
     * @param size 页面大小
     * @param searchRouteDto 查询条件
     * @return 自定义返回
     */
    @GetMapping("/getRouteList/{page}/{size}")
    public ResponseCode getRouteList(
            //           前台注释                           前台值传入变量
            @ApiParam(name = "page" ,value = "当前页码" ,required = true) @PathVariable Long page,
            @ApiParam(name = "size" ,value = "页码大小" ,required = true) @PathVariable Long size,
            @ApiParam(name = "searchRoutDto",value = "查询线路条件" ,required = false)  SearchRouteDto searchRouteDto){
        IPage<Route> routePage = new Page<>(page,size);
        Map<String,Object> map = routeService.getRouteList(routePage,searchRouteDto);
        return ResponseCode.success().data(map);
    }


}

